# coding=utf-8
__author__ = 'liubin'

from sqlalchemy import Column, Integer, String, Text, ForeignKey, Enum, Numeric
from sqlalchemy.orm import relationship
from project import db
from project.tools import order_states

class Order(db.Model):
    """
    订单表
    """
    __tablename__ = 'order_tab'
    id = Column(Integer, primary_key=True)
    customer = Column(String(50), nullable=False)
    tel = Column(String(50), nullable=False)
    comment = Column(Text)
    amount = Column(Numeric, nullable=False)

    def __init__(self, customer=None, tel=None, comment=None, amount=None):
        self.customer = customer
        self.tel = tel
        self.comment = comment
        self.amount = amount

    def __unicode__(self):
        return 'id:%s amount:%f' % (self.id, self.amount)


class OrderDetail(db.Model):
    """
    订单明细表
    """
    __tablename__ = 'order_detail_tab'
    id = Column(Integer, primary_key=True)
    count = Column(Integer, nullable=False)
    item_id = Column(Integer, ForeignKey('item_tab.id'))
    item = relationship("Item")
    order_id = Column(Integer, ForeignKey('order_tab.id'))
    order = relationship("Order", backref="details")
    order_state = Column(Enum(*order_states.keys()), nullable=False)

    def __init__(self, count=None, order_state='ordered' ):
        self.count = count
        self.order_state = order_state

    def __unicode__(self):
        return 'item:%s count:%f' % (self.item.name, self.count)